package com.i2863.shiroDemo.config;

import com.i2863.shiroDemo.dto.UserInfo;
import com.i2863.shiroDemo.service.ILogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Aspect
@Component
@Slf4j
public class SysLogAspect {

    // private ThreadLocal<Long> longThreadLocal = new ThreadLocal<>();

    @Resource
    private ILogService ilogService;

    @Pointcut("execution(public * com.i2863.shiroDemo.web..*.*(..))")
    public void webLog() {
    }

    @Before("webLog()")
    public void doBefore(JoinPoint joinPoint) {
        // longThreadLocal.set(System.currentTimeMillis());
        UserInfo userInfo = (UserInfo) SecurityUtils.getSubject().getPrincipal();
        // log.info("METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
        ilogService.addLog(joinPoint, userInfo);
    }

    @AfterReturning(returning = "ret", pointcut = "webLog()")
    public void doAfterReturning(Object ret) {
        // 处理完请求，返回内容
        //log.info("RESPONSE : " + ret);
        // log.info("SPEND TIME : " + (System.currentTimeMillis() - longThreadLocal.get()));
        // longThreadLocal.remove();
    }

}